package com.esms.service.impl;

import com.esms.dao.EmployeeMapper;
import com.esms.dao.LogValueMapper;
import com.esms.po.Employee;
import com.esms.po.LogValue;
import com.esms.po.Responsibility;
import com.esms.service.ILogService;
import com.esms.utils.PageUtils;
import com.esms.utils.convert.Time2StringConverter;
import com.esms.vo.LogVo;
import com.esms.vo.LogPagesVo;
import com.esms.vo.ResponsibilityDepVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.converter.ConverterFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author : [ok]
 * @version : [v1.0]
 * @description : [一句话描述该类的功能]
 * @createTime : [2022/3/21 12:03]
 * @updateUser : [ok]
 * @updateTime : [2022/3/21 12:03]
 * @updateRemark : [说明本次修改内容]
 */
@Service
public class LogValueServiceImpl implements ILogService {

	@Autowired
	private LogValueMapper logValueMapper = null;

	@Autowired
	private EmployeeMapper employeeMapper;

	@Override
	public int insert(LogValue record) {
		// TODO Auto-generated method stub
		return logValueMapper.insert(record);
	}

	@Override
	public void addLog(String className, String MethodName, Integer account, String value) {
		Timestamp date = new Timestamp(new Date().getTime());
		LogValue record = new LogValue();
		Employee employee = employeeMapper.selectByPrimaryKey(account);
		record.setLogAccount(employee.geteAccount());
		record.setLogClass(className);
		record.setLogMethod(MethodName);
		record.setLogValue("工号"+employee.geteAccount()+"员工,于"+date+value);
		record.setCreateTime(date);
		record.setUpdateTime(date);
		logValueMapper.insert(record);
	}

	@Override
	public LogPagesVo PageLogInfo(Integer pageSize, Integer pageNum, String account, String preTime,String aftTime) {
		MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
		ConverterFactory converterFactory = mapperFactory.getConverterFactory();
		converterFactory.registerConverter("Time2StringConverter", new Time2StringConverter());
		mapperFactory.classMap(LogValue.class, LogVo.class)
				.fieldMap("createTime","createTime").converter("Time2StringConverter").add()
				.byDefault().register();
		MapperFacade facade = mapperFactory.getMapperFacade();
		PageHelper.startPage(pageNum,pageSize);
		List<LogValue> logValues = logValueMapper.List(account, preTime,aftTime);
		PageInfo pageResult = new PageInfo(logValues);
		pageResult.getTotal();
		LogPagesVo pagesVo = new LogPagesVo();
		pagesVo.setCode(0);
		pagesVo.setMsg("");
		pagesVo.setData(facade.mapAsList(logValues, LogVo.class));
		pagesVo.setCount((int)pageResult.getTotal());
		return pagesVo;
	}

}
